package org.flashIso.engine.objects
{
	import flash.display.FrameLabel;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Scene;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import org.flashIso.engine.core.ILibrary;
	import org.flashIso.engine.core.ISerializableXML;
	import org.flashIso.engine.core.ValidateableSprite;
	import org.flashIso.engine.events.IsoEvent;
	import org.flashIso.engine.library.objects.LImage;
	
	public class IsoImage extends ValidateableSprite implements ISerializableXML
	{
		protected var _libraryReference : LImage;
		protected var _loader:Loader;
		private var _frame:String;
		
		public function IsoImage()
		{
			super();
		}
		
		public function set libraryReference(value:LImage) : void {
			if (_libraryReference != value){
				if (_libraryReference != null){
					_libraryReference.removeEventListener(Event.CHANGE, libraryReferenceChange);
				}
				_libraryReference = value;
				if (_libraryReference != null){
					_libraryReference.addEventListener(Event.CHANGE, libraryReferenceChange, false, 0, true);
				}
				libraryReferenceChange();
			}
		}
		
		public function get libraryReference() : LImage {
			return _libraryReference;
		}
		
		protected function libraryReferenceChange(e:Event = null) : void {
			if (_loader != null){
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadCompleteHandler);
				removeChild(_loader);
			}
			_loader = new Loader();
			addChild(_loader);
			_loader.contentLoaderInfo.addEventListener(Event.INIT, loadCompleteHandler, false, 0, true);
			var loaderContext:LoaderContext = new LoaderContext();
			loaderContext.allowCodeImport = true;
			
			if (_libraryReference != null){
				if (_libraryReference.data != null){
					_loader.loadBytes(_libraryReference.data, loaderContext);
				}
			}
//			invalidate();
		}
		
		protected function loadCompleteHandler(e:Event) : void {
			applyFrame();
			triggerValidation();
		}
		
		public function set frame(value:String) : void {
			if (_frame != value) {
				_frame = value;
				applyFrame();
			}
		}
		private function applyFrame() :void {
			if (_frame != null && _frame.length > 0 && movieClip != null) {
				try{
					movieClip.gotoAndStop(_frame);
				}catch (e:Error) {
				}
			}
		}
		public function get frame() : String {
			return _frame;
		}
		
		public function get movieClip() : MovieClip {
			if ((_loader != null) &&(_loader.content != null) && (_loader.content is MovieClip)) {
				return (_loader.content as MovieClip);
			}
			return null;
		}
		
		public function get loader() : Loader {
			return _loader;
		}
		
		override public function set scaleX(value:Number) : void {
			if (super.scaleX != value){
				super.scaleX = value;
				triggerValidation();
			}
		}
		override public function set scaleY(value:Number) : void {
			if (super.scaleY != value){
				super.scaleY = value;
				triggerValidation();
			}
		}
		override public function set rotation(value:Number) : void {
			if (super.rotation != value){
				super.rotation = value;
				triggerValidation();
			}
		}
		override public function set alpha(value:Number) : void {
			if (super.alpha != value){
				super.alpha = value;
				triggerValidation();
			}
		}
		override public function set x(value:Number) : void {
			if (super.x != value){
				super.x = value;
				triggerValidation();
			}
		}
		override public function set y(value:Number) : void {
			if (super.y != value){
				super.y = value;
				triggerValidation();
			}
		}
		
		public function toXML() : XML{
			var xml:XML = new XML("<" + className + "  />");
			xml.@x = x;
			xml.@y = y;
			xml.@alpha = alpha;
			xml.@rotation = rotation;
			xml.@scaleX = scaleX;
			xml.@scaleY = scaleY;
			
			xml.@frame = frame;
			
			if (libraryReference != null) {
				xml.@libraryID = libraryReference.id
			}		
			
			return xml;
		}
		
		public function fromXML(xml:XML, library:ILibrary = null) : void {
			if (library != null){
				libraryReference = library.getLImageByID(xml.@libraryID + "");
			}
			x = xml.@x;
			y = xml.@y;
			alpha = xml.@alpha;
			rotation = xml.@rotation;
			scaleX = xml.@scaleX;
			scaleY = xml.@scaleY;
			frame = xml.@frame;			 
		}
	}
}